package com.minelx.woods.server.entity;

import com.minelx.woods.server.entity.values.ReviewValues;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class ReviewsHelper {
	private final Reviews reviews;

	private final Review firstReview;

	private final Review secondReview;

	private final String firstReviewerId;

	private final String secondReviewerId;

	private final List<String> reviewers;

	private final String submitterId;

	public ReviewsHelper(TasksHelper helper) {
		reviews = new Reviews(helper.getDatabase().getCollection("reviews"));

		// setting up
		reviews.removeAll();

		firstReviewerId = "first reviewer";
		secondReviewerId = "second reviewer";
		reviewers = Arrays.asList(firstReviewerId, secondReviewerId);

		submitterId = "submitterId";
		firstReview = reviews.createReview(submitterId, "firstReviewName", helper.getFirstTaskValues().getId(), reviewers);
		secondReview = reviews.createReview(submitterId, "secondReviewName", helper.getSecondTaskValues().getId(), reviewers);
	}

	public String getSubmitterId() {
		return submitterId;
	}

	public List<String> getReviewers() {
		return reviewers;
	}

	public Reviews getReviews() {
		return reviews;
	}

	public Review getFirstReview() {
		return firstReview;
	}

	public Review getSecondReview() {
		return secondReview;
	}

	public String getFirstReviewerId() {
		return firstReviewerId;
	}

	public String getSecondReviewerId() {
		return secondReviewerId;
	}

	public void assertRejected(List<String> approved) {
		ReviewValues values = firstReview.values();
		assertEquals(approved, values.getApproved());
		assertEquals(reviewers, values.getReviewers());
		assertEquals(submitterId, values.getSubmitterId());
		assertFalse(values.completed());
		assertTrue(values.isRejected());
	}

	public void assertCompleted() {
		ReviewValues values = getFirstReview().values();
		assertEquals(getReviewers(), values.getApproved());
		assertEquals(getReviewers(), values.getReviewers());
		assertEquals(getSubmitterId(), values.getSubmitterId());
		assertTrue(values.completed());
	}

	public void assertPending(List<String> approved) {
		ReviewValues values = firstReview.values();
		assertEquals(approved, values.getApproved());
		assertEquals(getReviewers(), values.getReviewers());
		assertEquals(getSubmitterId(), values.getSubmitterId());
		assertFalse(values.completed());
	}

	public boolean approvedByFirstReviewer() {
		return approveBy(firstReviewerId);
	}

	public boolean approveBySecondReviewer() {
		return approveBy(secondReviewerId);
	}

	public void rejectByFirstReviewer() {
		rejectBy(firstReviewerId);
	}

	public void rejectBy(String reviewerId) {
		firstReview.reject(reviewerId);
	}

	public boolean approveBy(String reviewerId) {
		return firstReview.approve(reviewerId);
	}
}
